#include <iostream>

/**
 * Include the general SpaceGame header
**/
#include <SpaceGame/spacegame.h>

/**
 * Include Irrlicht libraries
**/
#include <irrlicht.h>

/**
 * Include the assocaited header
**/
#include <interface_irrlicht.h>


IrrlichtInterface::IrrlichtInterface(int width, int height)
{
    m_device = irr::createDevice(
        irr::video::EDT_OPENGL,                           /* Device Type    */
        irr::core ::dimension2d<irr::s32>(width, height), /* Screen Size    */
        16,                                               /* Stencil Depth  */
        false,                                            /* Fullscreen     */
        false,                                            /* Stencilbuffer  */
        false,                                            /* Vertical Sync  */
        NULL                                              /* Event Receiver */
    );

//  irr::video::IVideoDriver    * video = m_device->getVideoDriver();
//  irr::scene::ISceneManager   * scene = m_device->getSceneManager();
    irr::gui  ::IGUIEnvironment * guien = m_device->getGUIEnvironment();

    m_device->setWindowCaption( L"SpaceGame Engine" );

    guien->addStaticText(
        L"Hello world!",
        irr::core::rect<int>(10,10,200,22),
        true
    );
}

IrrlichtInterface::~IrrlichtInterface()
{
    m_device->drop();
}

bool IrrlichtInterface::isRunning()
{
    return m_device->run();
}

void IrrlichtInterface::tempLoopBody()
{
    irr::video::IVideoDriver    * video = m_device->getVideoDriver();
    irr::scene::ISceneManager   * scene = m_device->getSceneManager();
    irr::gui  ::IGUIEnvironment * guien = m_device->getGUIEnvironment();

    video->beginScene(true, true, irr::video::SColor(255,100,101,140));

    scene->drawAll();
    guien->drawAll();

    puts("loop");

    video->endScene();
}

/**
 * LUA OPERATOR
**/
LuaInterface::ostream& operator << (LuaInterface::ostream& os, IrrlichtInterface*& value) {
    void * irrlicht;
    os << irrlicht;
    value = (IrrlichtInterface*)irrlicht;

    return os;
}
LuaInterface::istream& operator >> (LuaInterface::istream& is, IrrlichtInterface* value) {
    void * irrlicht = value;
    return (is >> irrlicht);
}

/**
 * LUA BINDINGS
**/
void lua_construct(LuaInterface::istream& returns, LuaInterface::ostream& parameters) {
    IrrlichtInterface* irrlicht;
    float width, height;

    parameters << width << height;
    irrlicht = new IrrlichtInterface((int)width, (int)height);

    returns >> irrlicht;
}

void lua_tempLoopBody(LuaInterface::istream& returns, LuaInterface::ostream& parameters) {
    IrrlichtInterface * irrlicht;
    parameters << irrlicht;

    irrlicht->tempLoopBody();
}

void lua_isRunning(LuaInterface::istream& returns, LuaInterface::ostream& parameters) {
    IrrlichtInterface * irrlicht;
    parameters << irrlicht;

    returns >> irrlicht->isRunning();
}



/**
 * LUA REGISTER
**/
void IrrlichtInterface::registerMe(LuaInterface& lua) {
    lua.registerFunction("IrrlichtInterface_construct",    &lua_construct    );
    lua.registerFunction("IrrlichtInterface_tempLoopBody", &lua_tempLoopBody );
    lua.registerFunction("IrrlichtInterface_isRunning",    &lua_isRunning    );
}
